#pragma once
#include<stdbool.h>
#include<iostream>
#include<assert.h>
using namespace std;

namespace hu
{

	//----------------------------------------------------------------------------------------------------------

	template<class T>
	struct List_node
	{
		List_node(const T& val=T())
		:next(nullptr)
		,prev(nullptr)
		,_val(val)
		{}

		List_node<T>* next;
		List_node<T>* prev;
		T _val;
	};


	//----------------------------------------------------------------------------------------------------------

	template<class T,class cur,class ptr>
	struct List_iterator
	{	
		typedef List_node<T> node;
		typedef List_iterator<T, cur, ptr> self;
		node* _node;



		List_iterator(node* pos)
			:_node(pos)
		{}
		cur operator*()
		{
			return _node->_val;
		}
		self& operator++()
		{
			_node = _node->next;
				return *this;
		}

		self& operator++(int)
		{
			self tmp(*this);
			_node = _node->next;
			return tmp;
		}

		self& operator--()
		{
			_node = _node->prev;
			return *this;
		}

		self& operator--(int)
		{
			self tmp(*this);
			_node = _node->prev;
			return tmp;
		}

		const bool operator==( const self& tmp) 
		{
			return _node == tmp._node;
		}
		const bool operator!=( const self& tmp) 
		{
			return _node != tmp._node;
		}
	};

	
	//----------------------------------------------------------------------------------------------------------
	template<class T>
	class list
	{
		typedef List_node<T> node;
	public:
		typedef List_iterator<T,const T&,const T*> const_iterator;
		typedef List_iterator<T,T&,T*> iterator;
		list()
		{
			head = new node;
			head->prev = head;
			head->next = head;

		}

		list(list<T>& tmp)
		{
			head = new node;
			head->prev = head;
			head->next = head;
			for (auto& e : tmp)
			{
				push_back(e);
			}
		}
		~list()
		{
			clear();
			head = nullptr;
			_size = 0;
		}

		

		iterator begin()
		{
			return head->next;
		}
		iterator end()
		{
			return head;
		}
		iterator insert(iterator pos, T x)
		{
			node* cur = pos._node;
			node* prev = cur->prev;
			node* newnode = new node(x);
			cur->prev = newnode;
			prev->next = newnode;
			newnode->prev = prev;
			newnode->next = cur;
			_size++;
			return newnode;
		}
		iterator erase(iterator pos)
		{
			assert(pos != end());
			node* cur = pos._node;
			node* prev = cur->prev;
			node* next = cur->next;
			prev->next = next;
			next->prev = prev;
			delete cur;
			_size--;
			return next;
		}


		void clear()
		{
			iterator it = begin();
			while (it != end())
			{
				it = erase(it);
			}
		}


		void swap(list<T>& lt)
		{
			std::swap(head, lt.head);
			std::swap(_size, lt._size);
		}

		list<T>& operator=(list<T> lt)
		{
			swap(lt);
			return *this;
		}



		int size()
		{
			return _size;
		}

		void push_back(const T& val)
		{
			insert(end(), val);
		}

		void pop_back()
		{
			erase(--end());
		}

		void pop_front()
		{
			erase(begin());
		}

		void push_front(const T& val)
		{
			insert(begin(), val);
		}


		void print()
		{
			iterator it = begin();
			while (it != end())
			{
				cout << *it << "->";
				it++;
			}
		}
	private:
		node* head;
		int _size;
	};
}